package com.example.questionsystem.modules.qu.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.questionsystem.modules.qu.entity.Projects;
import com.example.questionsystem.modules.qu.entity.SubjectSessions;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class NestedJsonParserUtil {

    /**
     * 从文件中读取 JSON 内容并解析
     * @param filePath JSON 文件的路径
     * @return 解析后的 JSONObject
     */
    public static JSONObject readJsonFromFile(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            StringBuilder jsonContent = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                jsonContent.append(line);
            }
            return JSONObject.parseObject(jsonContent.toString());
        } catch (IOException e) {
            System.err.println("读取文件出错: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("解析 JSON 出错: " + e.getMessage());
        }
        return null;
    }

    /**
     * 根据路径从 JSON 对象中获取值
     * @param jsonObject 要解析的 JSON 对象
     * @param path 要查找的路径，用点号分隔，例如 "person.address.city"
     * @return 查找到的值，如果未找到则返回 null
     */
    public static Object getValueByPath(JSONObject jsonObject, String path) {
        String[] keys = path.split("\\.");
        Object current = jsonObject;
        for (String key : keys) {
            if (current instanceof JSONObject) {
                current = ((JSONObject) current).get(key);
            } else if (current instanceof JSONArray) {
                try {
                    int index = Integer.parseInt(key);
                    current = ((JSONArray) current).get(index);
                } catch (NumberFormatException e) {
                    return null;
                }
            } else {
                return null;
            }
            if (current == null) {
                return null;
            }
        }
        return current;
    }

    /**
     * 批量根据路径从 JSON 对象中获取值
     * @param jsonObject 要解析的 JSON 对象
     * @param paths 要查找的路径列表
     * @return 包含路径和对应值的映射
     */
    public static java.util.Map<String, Object> getValuesByPaths(JSONObject jsonObject, List<String> paths) {
        java.util.Map<String, Object> result = new java.util.HashMap<>();
        for (String path : paths) {
            Object value = getValueByPath(jsonObject, path);
            result.put(path, value);
        }
        return result;
    }

    public static void main(String[] args) {
        String filePath = "C:\\Users\\Administrator\\Desktop\\考前模板.txt";
        JSONObject jsonObject = readJsonFromFile(filePath);
        if (jsonObject != null) {
            List<String> paths = new ArrayList<>();
            paths.add("kszb.ksmc");
            paths.add("listKckm");
            paths.add("listKccckmfl");
            paths.add("listKccckm");
            java.util.Map<String, Object> values = getValuesByPaths(jsonObject, paths);
            Projects projects = new Projects();
            projects.setProjectId(UUID.randomUUID().toString());
            projects.setProjectName((String) values.get("kszb.ksmc"));
            projects.setSubjectCount(((JSONArray) values.get("listKckm")).size());
            System.out.println(projects);
            ArrayList<SubjectSessions> SubjectSessionsList = new ArrayList<>();
            if (values.get("listKccckm") != null){
                for (int i = 0; i < ((JSONArray) values.get("listKccckm")).size(); i++) {
                    System.out.println(((JSONArray) values.get("listKccckm")).getJSONObject(i).get("kcmc"));
                    SubjectSessions subjectSessions = new SubjectSessions();
                    subjectSessions.setProjectId(projects.getProjectId());
                    subjectSessions.setSubjectName((String) ((JSONArray) values.get("listKccckm")).getJSONObject(i).get("kmmc"));
                    subjectSessions.setSessionName((String) ((JSONArray) values.get("listKccckm")).getJSONObject(i).get("ccmc"));
                    Date date = ((JSONArray) values.get("listKccckm")).getJSONObject(i).getDate("kskssj");
                    System.out.println(date
                    );
                    //subjectSessions.setStartTime(((JSONArray) values.get("listKccckm")).getJSONObject(i).getDate("kskssj"));
                    //subjectSessions.setEndTime(((JSONArray) values.get("listKccckm")).getJSONObject(i).getDate("ksjssj"));
                    SubjectSessionsList.add(subjectSessions);
                }
            }
            System.out.println(SubjectSessionsList);



        }
    }
}